<?php

namespace App\Http\Middleware;

use Closure;
// use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Models\OperationLog;

class OperationLog
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $userId = 0;
        if(Auth::check()) {
            $userId = (int) Auth::id();
        }
        if('GET' != $request->method()){
            $input = $request->all();
            $log = new OperationLog(); # 提前创建表、model
            $log->userid = auth()->user()->userid;
    		$log->username = auth()->user()->username;
            $log->path = $request->path();
            $log->method = $request->method();
            $log->ip = $request->ip();
            $log->sql = '';
            $log->content = json_encode($input, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
            $log->save();   # 记录日志
        }
        return $next($request);
    }
}
